#include "xrtc/rtc/modules/rtp_rtcp/rtp_header_extension_map.h"
#include <absl/strings/string_view.h>
#include <rtc_base/arraysize.h>
#include <rtc_base/logging.h>
#include "xrtc/rtc/modules/rtp_rtcp/rtp_header_extensions.h"
namespace xrtc{
	namespace {

		struct ExtensionInfo {
			RTPExtensionType type;
			absl::string_view uri;

		};
		template <typename Extension>
		const ExtensionInfo CreateExtensionInfo() {
			return { Extension::kId,Extension::Uri() };
		}
		const ExtensionInfo kExtensions[] = {
		CreateExtensionInfo<TransportSequenceNumber>(),
		};
		static_assert(arraysize(kExtensions) == static_cast<int>(kRtpExtensionNumberOfExtensions) - 1,
			"kExtensions expect to list all know extensions");
		 
	} //namespace 

	RtpHeaderExtensionMap::RtpHeaderExtensionMap() {
		for (auto& id : ids_)
		{
			id = kInvalidId;
		}
	}

	bool RtpHeaderExtensionMap::RegisterUri(int id, absl::string_view uri)
	{
		for (const ExtensionInfo& extension : kExtensions)
		{	
			if (extension.uri == uri)
			{
				return Register(id, extension.type, extension.uri);
			}
		
		}
		RTC_LOG(LS_WARNING) << "unknown extension uri:" << uri << ",id:" << id;
		return false;
	}

	RTPExtensionType RtpHeaderExtensionMap::GetType(int id) const
	{
		for (int type = kRtpExtensionNone; type < kRtpExtensionNumberOfExtensions; ++type)
		{
			if (ids_[type] == id) {
			
				return static_cast<RTPExtensionType>(type);
			}
		}
		return kRtpExtensionNone;
	}

	uint8_t RtpHeaderExtensionMap::GetId(RTPExtensionType type) const
	{
		return ids_[type];
	}

	bool RtpHeaderExtensionMap::Register(int id, RTPExtensionType type, absl::string_view uri)
	{
		// id =>[1,255]

		if (id<webrtc::RtpExtension::kMinId || id>webrtc::RtpExtension::kMaxId)
		{
			RTC_LOG(LS_WARNING) << "failed to register extension uri: " << uri << " with invlaid id: " << id;
			return false;
		}
	
		RTPExtensionType  registered_type = GetType(id);
		if (registered_type == type)
		{
			RTC_LOG(LS_INFO) << "Externsion already registered,uri: " << uri << ",id: " << id << ",type: " << static_cast<int>(type);
			return true;
		}
		if (registered_type != kRtpExtensionNone)
		{
			RTC_LOG(LS_WARNING) << "Extension type already used ,uri:  " << uri
				<< ", id: " << id
				<< ",type: " << static_cast<int>(type);
			return false;
		}
		ids_[type] = static_cast<uint8_t>(id);

		return true;
	}

}//namespace xrtc